package Server;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class ScheduledExecutor implements Runnable {
    private Connection connection = null;
    private PreparedStatement pStmt = null;
    private ResultSet rs = null;

    ScheduledExecutor() {
        try {
            Class.forName(DB.DRIVER);
            connection = DriverManager.getConnection(DB.URL, DB.USER, DB.PASSWORD);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        Map<String, Integer> roomList = new HashMap<>();
        try {
//            System.out.println("test running or not");
            String sql = "SELECT room_id ,room_power FROM room WHERE  room_occupy=true";//房间被占用
            pStmt = connection.prepareStatement(sql);
            rs = pStmt.executeQuery();
            while (rs.next()) {
                roomList.put(rs.getString("room_id"), rs.getInt("room_power"));
            }
            for (String roomId : roomList.keySet()) {
                String sql1 = "SELECT room_occupy, room_now_temp, room_set_temp, room_set_wind, room_fee,room_totalfee" +
                        " FROM room WHERE room_id=? ";
                pStmt = connection.prepareStatement(sql1);
                pStmt.setString(1, roomId);
                rs = pStmt.executeQuery();
                if (rs.next()) {
                    boolean room_occupy = rs.getBoolean("room_occupy");
                    double room_now_temp = rs.getDouble("room_now_temp");
                    double room_set_temp = rs.getDouble("room_set_temp");
                    int room_set_wind = rs.getInt("room_set_wind");
                    double room_fee = rs.getDouble("room_fee");
                    double room_totalfee = rs.getDouble("room_totalfee");
                    double addFee = 0;
                    if (room_set_wind == 1) {
                        addFee = (Server.FEE / 3) / (60.0 / Server.TIMEPERIOD);
                    }
                    else if (room_set_wind == 2) {
                        addFee = (Server.FEE / 2) / (60.0 / Server.TIMEPERIOD);
                    }
                    else if (room_set_wind == 3) {
                        addFee = Server.FEE / (60.0 / Server.TIMEPERIOD);
                    }
                    room_fee += addFee;
                    room_totalfee += addFee;
                    if (roomList.get(roomId) == 1) {//仅对空调打开即power为1的房间费用做更新操作
                        String roomsql = "UPDATE room SET room_fee=?, room_totalfee=? WHERE room_id=?";//更新room表中的room_fee
                        PreparedStatement pStmt2 = connection.prepareStatement(roomsql);
                        pStmt2.setDouble(1, room_fee);
                        pStmt2.setDouble(2, room_totalfee);
                        pStmt2.setString(3, roomId);
                        pStmt2.executeUpdate();
                    }
                    //插入更新bill
                    String billsql2 = "INSERT INTO bill VALUES (LOCALTIME(), ?, ?, ?, ?, ?, ?)";
                    PreparedStatement pStmtbill;
                    pStmtbill = connection.prepareStatement(billsql2);
                    pStmtbill.setString(1, roomId);
                    pStmtbill.setBoolean(2, room_occupy);
                    pStmtbill.setDouble(3, room_now_temp);
                    pStmtbill.setDouble(4, room_set_temp);
                    pStmtbill.setInt(5, room_set_wind);
                    pStmtbill.setDouble(6, room_fee);
                    pStmtbill.executeUpdate();
                }
            }
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            try {
                if (pStmt != null) {
                    pStmt.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    //TODO:修改计费策略

}
